package tg.app.opticket;

import java.io.PrintStream;
import java.rmi.RemoteException;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.mbo.MboValue;
import psdi.util.MXException;

/***
 * 
 * @author pzx
 *
 */
public class FldJpnum  extends psdi.app.jobplan.FldJpnum
{
  String changeSafetyPlan = "CANCEL";
  String newSafetyPlanID = "";
  boolean hadSafetyPlan = true;
  
  public FldJpnum(MboValue mbv)
    throws MXException, RemoteException
  {
    super(mbv);
    String thisAttr = getMboValue().getAttributeName();
    setRelationship("JOBPLAN", "JPNUM=:" + thisAttr);
    setListCriteria("status in (select value from synonymdomain where (maxvalue='ACTIVE' or maxvalue='DRAFT') and domainid='JOBPLANSTATUS')");
    String[] FromStr = { "JPNUM" };
    String[] ToStr = { thisAttr };
    setLookupKeyMapInOrder(ToStr, FromStr);
  }
  
  public void action()
    throws MXException, RemoteException
  {
    if (!getMboValue().isNull()){
      MboRemote mbo = getMboValue().getMbo();
      MboSetRemote memset = mbo.getMboSet("JOBPLAN");
      if (!memset.isEmpty()){
        MboRemote mem = memset.getMbo(0);
        String jobplanid = mem.getString("JOBPLANID");
        mbo.setValue("JOBPLANID", jobplanid);
      }
      if (mbo.getInt("JOBPLANID") != 0.0D) {
        MboSetRemote jbtkset = mbo.getMboSet("JOBTASK");
        MboSetRemote optset = mbo.getMboSet("OPTASK");
        if (!jbtkset.isEmpty()) {
            jbtkset.setOrderBy("TASKID desc");
          for (int i = 0; i < jbtkset.count(); i++){
            MboRemote sourcePRLine = jbtkset.getMbo(i);
            // System.out.println("Value = " + sourcePRLine.getString("JPNUM"));
            OpTask balline = (OpTask)optset.add();
            balline.setValue("TASKID", sourcePRLine.getString("JPTASK"), 2L);
            balline.setValue("DESCRIPTION", sourcePRLine.getString("DESCRIPTION"), 2L);
          }
        }
      }
    }
    super.action();
  }
  
}
